﻿// C# example:
using UnityEngine;
using UnityEditor;
using System;
using System.Threading;

public class Autosave : EditorWindow
{
    private bool _autosavingEnabled = false;
    private int _minutesBetweenSaves = 10;

    private int minutesBetweenSaves
    {
        get { return _minutesBetweenSaves; }
        set 
        {
            if (_minutesBetweenSaves < 1)
                _minutesBetweenSaves = 1;

            UnityEditor.EditorPrefs.SetInt("RowAutosaveSettingsTime", value);
            _minutesBetweenSaves = value;
        }
    }

    private bool autosavingEnabled 
    {
        get { return _autosavingEnabled; } 
        set 
        {
            if (_autosavingEnabled != value)
            {
                UnityEditor.EditorPrefs.SetBool("RowAutosaveSettingsEnabled", value);
                time = 0;
                _autosavingEnabled = value;
            }
        }
    }

    private DateTime lastSavingDate;
    private int time = 0;
    
    void OnInspectorUpdate()
    {
        if (autosavingEnabled && time++ > 100)
        {
            //Check every 10 secs
            DateTime now = DateTime.UtcNow;
            if ((now - lastSavingDate).TotalMinutes >= minutesBetweenSaves)
            {
                if (!System.IO.Directory.Exists("Backup"))
                    System.IO.Directory.CreateDirectory("Backup");

                string strDate =  "" + now.Day + "-" + now.Month + "-" + now.Year;
                string strHour = "" + now.Hour + "h" + now.Minute + "m";
                string sceneName = GetSceneName(EditorApplication.currentScene);
                EditorApplication.SaveScene("Backup/" + sceneName + " " 
                    + strDate + " " + strHour + ".Scene.unity");
                lastSavingDate = now;
            }

            time = 0;
        }
    }

    private string GetSceneName(string fullName)
    {
        if (fullName.Length == 0)
            return "Untitled";

        char[] c = { '\\', '/', '.' };
        //Chop when \ / and .
        string[] ret = fullName.Split(c);
        //Get "SceneName" in "/path/to/project/Assets/SceneName.Scene.unity"
        return ret[ret.Length - 3];
    }

    [MenuItem("Edit/Autosave Settings")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        Autosave window = (Autosave)EditorWindow.GetWindow(typeof(Autosave));
        window.Show();
    }

    public Autosave()
    {
        lastSavingDate = DateTime.UtcNow;
        _autosavingEnabled = UnityEditor.EditorPrefs.GetBool("RowAutosaveSettingsEnabled");
        _minutesBetweenSaves = UnityEditor.EditorPrefs.GetInt("RowAutosaveSettingsTime");
    }

    void OnGUI()
    {
        GUILayout.Label("Your autosaved Scenes will be saved in Assets/Backup/");

        autosavingEnabled = EditorGUILayout.BeginToggleGroup("Enabled", autosavingEnabled);
        minutesBetweenSaves = EditorGUILayout.IntField("  Autosave minutes", minutesBetweenSaves);
        EditorGUILayout.EndToggleGroup();
    }
}
